/**
 * Created by Administrator on 2016/9/22.
 */
$(function(){
//   点击选择样本
   $(document).on('click','.samplelist .list .con .bot .left',function(){
       $(this).addClass('hover').find('img').attr('src','images/icon6.png').parents('.list').siblings().find('.bot').children('.left').removeClass('hover').children('img').attr('src','images/icon5.png');
       var id = $(this).data('id');
       var data = {
           uid:$.cookie("local_userId"),
           sample_id:id
       }
       geneticTesting.get('wish/index/setDefaultGeneSample',data,function(res){
           if(res.ret==0){
               console.log(res);
               geneticTesting.propmt('设置默认成功');
           }
           else{
               console.log(res.msg);
           }
       });
       $(this).siblings('.right').hide().parents('.list').siblings().find('.bot').children('.right').show();
   });
//    删除默认样本
    $(document).on('click','.samplelist .list .con .bot .right',function(){
        $(this).parents('.list').fadeOut();
        var id = $(this).siblings('.left').data('id');
        var data = {
            uid:$.cookie("local_userId"),
            sample_id:id
        }
        geneticTesting.get('wish/index/delGeneSample',data,function(res){
            if(res.ret==0){
                console.log(res);
                geneticTesting.propmt('删除样本成功');
            }
            else{
                console.log(res.msg);
            }
        });
    });
//    支付方式选择
    $('.sub-order .order-detail .mode .right .public').click(function(){
        $(this).addClass('hover').siblings().removeClass('hover');
        $(this).find('.radio').attr('src','../hospital_nurse/images/icon41.png').parent().siblings().find('.radio').attr('src','../hospital_nurse/images/icon40.png');
    });
//    新建地址确定
    $('.newAddress .determine').click(function(){
        var address_id = geneticTesting.getQueryString('id');
        var name = $('.newAddress .con .name input').val();
        var phone = $('.newAddress .con .phone input').val();
        var add_infor = $('.newAddress .con .add-infor input').val();
        var type = geneticTesting.getQueryString('type');
        var door_id = geneticTesting.getQueryString('door_id');
        if(name==''){
            geneticTesting.layer('请输入收货人名字');
        }else if(phone==''){
            geneticTesting.layer('请输入收货人联系电话');
        }else if(!obj.phone.test(phone)){
            geneticTesting.layer('联系电话格式不正确');
        }else if(add_infor ==''){
            geneticTesting.layer('请输入详细收货地址');
        }else{
            if(address_id){
                var gene_id = geneticTesting.getQueryString('gene_id');

                var options = {
                    url: "http://ekang.ren/address/add",
                    target : '#output',
                    dataType:'json',
                    data:{
                        uid:$.cookie("local_userId"),
                        address_id:address_id
                    },
                    success: function (res) {
                        if(res.ret==0){
                            //location.href='editAdd.html'

                            if(type=='shop'){
                                location.href='../hospital_nurse/submitorder-shop.html?address_id='+res.data.address_id
                            }else if(type=='get_door_doctor'){
                                location.href='../hospital_nurse/submitorder-chineseMedicine.html?address_id='+res.data.address_id+'&id='+door_id
                            }else{
                                location.href='../hospital_nurse/submitorder.html?gene_id='+gene_id+'&address_id='+res.data.address_id
                            }

                            console.log(res)
                        }else{
                            console.log(res.msg)
                        }
                    }
                };
            }else{
                var gene_id = geneticTesting.getQueryString('gene_id');
                var options = {
                    url: "http://ekang.ren/address/add?uid="+$.cookie("local_userId"),
                    target : '#output',
                    dataType:'json',
                    success: function (res) {
                        if(res.ret==0){
                            console.log(res)
                            if(type=='shop'){
                                location.href='../hospital_nurse/submitorder-shop.html?address_id='+res.data.address_id
                            }else if(type=='get_door_doctor'){
                                location.href='../hospital_nurse/submitorder-chineseMedicine.html?address_id='+res.data.address_id+'&id='+door_id
                            }else{
                                location.href='../hospital_nurse/submitorder.html?gene_id='+gene_id+'&address_id='+res.data.address_id
                            }
                        }else{
                            console.log(res.msg)
                        }
                    }
                };
            }
            $("#form1").ajaxSubmit(options);
        }
    });
//    编辑地址滑动删除
    $(document).on('swipeleft','.editadd .addlist .con',function(){
        $(this).parent().animate({'left':'-20vw'});
    });
    $(document).on('swiperight','.editadd .addlist .con',function(){
        $(this).parent().animate({'left':'0'});
    });
//    radio切换
    $(document).on('touchstart','.editadd .addlist .radio',function(){
        $(this).attr('src','images/icon6.png').parents('.addlist').siblings().find('.radio').attr('src','images/icon5.png');
    });
//    删除地址列表
    $(document).on('touchstart','.editadd .addlist .del',function(){
        $(this).parent().animate({'left':'-120vw'},500,function(){
            $(this).remove();
        });
        var address_id = $(this).data('id');
        geneticTesting.get('address/del',{uid:$.cookie("local_userId"),address_id:address_id},function(res){
            if(res.ret==0){
                console.log(res);
            }
            else{
                console.log(res)
                console.log(res.msg);
            }
        });
    });
//    点击订单列表近乡情
    $(document).on('click','.order-list .list',function(){
        var id = $(this).data('id');
        location.href = 'orderdetail.html?id='+id;
    });
//    点击新增地址
    $('.editadd-button button').click(function(){
        var gene_id = geneticTesting.getQueryString('gene_id');
        var door_id = geneticTesting.getQueryString('door_id');
        var type = geneticTesting.getQueryString('type');
        if(type=='shop'){
            location.href = 'newAddress.html?type=shop'
        }else if(type=='get_door_doctor'){
            location.href = 'newAddress.html?type=get_door_doctor&door_id='+door_id
        }else{
            location.href = 'newAddress.html?gene_id='+gene_id
        }
    });
});
//正则验证
obj={
    "phone":/^(((13[0-9]{1})|(14[0-9]{1})|(17[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/,
    "email":/^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/,
    "name":/^[\u4e00-\u9fa5]+$/,
    "num":/^[1-9]+[0-9]*]*$/,
    "Idnumber":/^\d{17}[\w\d]$/,
};
//公用对象
url = 'http://ekang.ren/'
geneticTesting = {
//    链接
    link : function(url){
        location.href = url;
    },
//    获取连接参数
    getQueryString : function(name){
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(decodeURI(r[2])); return null;
    },
//    弹层
    layer : function (content){
        $('body').append('<div class="elasticlayer animatescale" style=" z-index: 9999999999999; position: fixed; top:0; width: 100%; height:100%; background:rgba(0,0,0,.7) "><div class="elasticlayercon" style=" background: rgba(255,255,255,1); border-radius: 5px; width: 70%;position: absolute; left: 50%; top: 50%; -webkit-transform:translate(-50%, -50%);"><div class="tit" style=" width: 100%; color:#35424b; text-align: center; padding: 5% 0; font-size: 5vw">易康医疗温馨提醒</div><p style=" width: 90%; margin: auto; text-align: center; font-size: 4.5vw; padding: 5% 0 10% 0; ">'+content+'</p><button style=" display: block; background: none; border: none; padding: 5% 0; font-size:5.5vw; color: #00b57d; width: 100%; border-top: 1px solid #ddd; letter-spacing: 1px;">ok</button></div></div>')
        $(document).on('click','.elasticlayer .elasticlayercon button',function(){
            $('.elasticlayer').remove();
            $('.publicmask').remove();
        });
    },
//    get
    get : function (texturl,data,success){
        $.ajax({
            type : 'get',
            url : url+texturl,
            dataType : 'jsonp',
            data : data,
            success : function(response) {
                success(response);
            }
        });
    },
//    首页接口
    index : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                new Vue({
                    el: '#gene',
                    data: {
                        tit: data.top.title,
                        desc : data.top.desc,
                        box : data.list
                    }
                });
            }
            else{
                console.log(res.msg);
            }
        });
    },
//    编辑地址
    newAddress : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                $('.newAddress .con .name input').val(data.user_name);
                $('.newAddress .con .phone input').val(data.mobile);
                $('.newAddress .con .add-infor input').val(data.address_desc);
            }
            else{
                console.log(res)
                console.log(res.msg);
            }
        });
    },
//    地址列表
    addlist : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                var gene_id = geneticTesting.getQueryString('gene_id');
                var door_id = geneticTesting.getQueryString('door_id');
                var type = geneticTesting.getQueryString('type');
                new Vue({
                    el: '#editadd',
                    data: {
                        list:data,
                        gene_id : gene_id
                    },
                    methods: {
                        link: function (id) {
                            if(type=='shop'){
                                location.href = 'newAddress.html?id='+id+'&type=shop'
                            }else if(type=='get_door_doctor'){
                                location.href = 'newAddress.html?id='+id+'&type=get_door_doctor'+'&door_id='+door_id
                            }else{
                                location.href = 'newAddress.html?id='+id+'&gene_id='+gene_id
                            }

                        },
                        link2: function (id) {
                            if(type=='shop'){
                                location.href = '../hospital_nurse/submitorder-shop.html?address_id='+id
                            }else if(type=='get_door_doctor'){
                                location.href = '../hospital_nurse/submitorder-chineseMedicine.html?address_id='+id+'&id='+door_id
                            }else{
                                location.href = '../hospital_nurse/submitorder.html?address_id='+id+'&gene_id='+gene_id
                            }

                        }
                    }
                });
            }
            else{
                console.log(res.msg);
            }
        });
    },
//    订单预览
    ord_preview : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                new Vue({
                    el: '#sub-order',
                    data: {
                        address_desc:data.address_desc,
                        user_name : data.user_name,
                        mobile : data.mobile,
                        total_amount: data.total_amount,
                        gene_name:data.gene_name,
                        gene_img_pic:data.gene_img_pic,
                        is_have_address : data.is_have_address,
                        address_id : data.address_id,
                        promotion_price : data.promotion_price
                    },
                    methods: {
                        link: function (id) {
                            location.href = 'editAdd.html?id='+id
                        }
                    }
                });
            }
            else{
                console.log(res.msg);
            }
        });
    },
//    我的dna
    mygene : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                $('.gene .con-box .userinfor .head-pic').attr('style',' background: url('+data.avatar_file+') center;background-size:cover')
                new Vue({
                    el: '#mydna',
                    data: {
                        model_num:data.model_num,
                        order_num:data.order_num,
                        desc : data.desc,
                        avatar_file:data.avatar_file,
                        user_name : data.user_name
                    }
                });
            }
            else{
                console.log(res.msg);
            }
        });
    },
//    订单列表
    orderlist : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var resdata = res.data;
                if(resdata.length<12){
                    $('.infinite-scroll-preloader').html('暂无加载数据')
                }
                if(data.page_index==1&&resdata.length<1){
                    geneticTesting.pagenull('还没有任何订单')
                }
                $.each(resdata,function(i,t){
                    $('#order-list').append('<div data-id="'+ t.order_id+'" class="list"><div class="number public"><span class="span_l">订单编号：</span><span style=" float:left;" class="span_r">'+t.order_no+'</span></div><div class="price"><div class="left"><img src="'+t.gene_img_pic+'"><p>'+t.gene_name+'</p></div></div><div class="status public"><span style="color: #999;" class="span_l">'+t.status_text+'</span><span style="color: #ff810c;" class="span_r">￥'+t.total_amount+'元</span></div></div>')
                });
            }
            else{
                console.log(res.msg);
            }
        });
    },
//    订单详情
    orderdetail : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                new Vue({
                    el: '#order-list',
                    data: {
                        gene_name:data.gene_name,
                        total_amount:data.total_amount,
                        num:data.num,
                        c_on:data.c_on,
                        status_text:data.status_text
                    }
                });
            }
            else{
                console.log(res.msg);
            }
        });
    },
//    样本列表
    samplelist : function(url,uid,page_size,page_index){
        geneticTesting.get(url,{uid:uid,page_size:page_size,page_index:page_index},function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                if(data.length<12){
                    $('.infinite-scroll-preloader').html('暂无加载数据')
                }
                if(page_index==1&&data.length<1){
                    geneticTesting.pagenull('还没有任何样本')
                }
                $.each(data,function(i,t){
                    $('#samplelist').append('<div class="list"><div class="con"><div class="infor"><p class="number public">样本编号：<label>'+ t.sample_no+'</label></p><p class="name public">样本姓名：<label>'+ t.sample_name+'</label></p><p class="type public">样本类型：<label>'+ t.sample_type+'</label></p></div><div class="bot"><div data-id="'+ t.sample_id+'" class="left public"><img src="images/icon5.png"><p>使用此默认样本</p></div><div class="right public"><img src="images/icon7.png"><p>删除</p></div></div></div></div>')
                });
                //    设置默认
            }
            else{
                console.log(res.msg);
            }
        });
    },
    //    样本列表
    samplelist2 : function(url,uid,page_size,page_index){
        geneticTesting.get(url,{uid:uid,page_size:page_size,page_index:page_index},function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;

                if(data.length<12){
                    $('.infinite-scroll-preloader').html('暂无加载数据')
                }
                if(page_index==1&&data.length<1){
                    geneticTesting.pagenull('还没有任何样本')
                }
                $.each(data,function(i,t){
                    $('#samplelist').append('<div class="list"><div class="con"><div class="infor"><p class="number public">样本编号：<label>'+ t.sample_no+'</label></p><p class="name public">样本姓名：<label>'+ t.sample_name+'</label></p><p class="type public">样本类型：<label>'+ t.sample_type+'</label></p></div><div class="bot"><div data-id="'+ t.sample_id+'" class="left public"><img src="images/icon5.png"><p>使用此默认样本</p></div><div class="right public"><img src="images/icon7.png"><p>删除</p></div></div></div></div>')
                });
                //    设置默认
                $('.samplelist .list').eq(0).find('.bot').children('.left').children().attr('src','images/icon6.png');
                $('.samplelist .list').eq(0).find('.bot').children('.right').hide();
            }
            else{
                console.log(res.msg);
            }
        });
    },
//    提示
    propmt :　function(desc){
        $('body').append('<div style="background: rgba(0,0,0,.7) ; color: #fff; min-width: 100px; border-radius: 5px; font-size: 16px; text-align: center; z-index:999999;width: auto; height: auto; padding: 10px 7px;position: absolute;" class="propmt animate_sc">'+desc+'</div>');
        var w_w = $(window).width();
        var h_h = $(window).height();
        var p_w = $('.propmt').width();
        var p_h = $('.propmt').height();
        var top = (h_h-p_h)/2;
        var left = (w_w-p_w)/2;
        $('.propmt').css('top',top);
        $('.propmt').css('left',left);
        setTimeout(function(){
            $('.propmt').remove();
        },1500);
    },
//    webViewJavaScript初始化
    connectWebViewJavascriptBridge :  function(callback) {
        if (window.WebViewJavascriptBridge) {
            callback(WebViewJavascriptBridge);
        } else {
            document.addEventListener('WebViewJavascriptBridgeReady', function() {callback(WebViewJavascriptBridge);}, false);
        }
    },
//    项目详情
    project_detail : function(url,data){
        geneticTesting.get(url,data,function(res){
            if(res.ret==0){
                console.log(res);
                var data = res.data;
                $('title').html(data.gene_name);
                $('.projecdetail .content').html(data.gene_intro);
            }
            else{
                console.log(res.msg);
            }
        });
    },
//    判断是否登录
    login : function(url,data){
        geneticTesting.get(url,data,function(res){
            console.log(res);
            if(res.errcode==6001){
                setInterval(function(){
                    location.href ='../login.html';
                },300);
            }
        });
    },
//    空白页面
    pagenull : function(desc){
        $('body').append('<div class="emptypage" style="position: fixed;top:0;width:100%;height:100%;overflow: hidden;z-index: 5000;background: #F5F5F9;"><div class="con" style="width: 256px;height: auto;overflow: hidden;margin: auto;left: 50%; top: 50%; -webkit-transform: translate(-50%, -50%); position: absolute"><img style="width: 114px;height: auto;overflow: hidden;margin: auto;" src="images/icon27.png"><p style="font-size: 14px;text-align: center;color: #35424b;margin: 20px 0;">'+desc+'</p></div></div>')
        $('.emptypage .con button').live('click',function(){
            location.href=url
        });
        $('.emptypage').on('touchmove', function (event) {
            event.preventDefault();
        });
    },
}